package coml.jiyun.hp.reviewtopic.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import coml.jiyun.hp.reviewtopic.DbUtil;
import coml.jiyun.hp.reviewtopic.R;
import coml.jiyun.hp.reviewtopic.base.BaseApp;
import coml.jiyun.hp.reviewtopic.bean.HomeBean;

public class RlvHomeAdapter extends RecyclerView.Adapter {

    private Context context;
    public List<HomeBean.DataBean.DatasBean> datasBeans = new ArrayList<>();
    private int left = 1;
    private int right = 2;

    public RlvHomeAdapter(Context context, ArrayList<HomeBean.DataBean.DatasBean> datasBeans1) {
        this.context = context;
    }

    public void setDatasBeans(List<HomeBean.DataBean.DatasBean> datasBeans) {
        this.datasBeans.addAll(datasBeans);

        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == left){
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_rlv_home_one, parent, false);
            return new ViewHolder1(inflate);
        }else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_rlv_home_two, parent, false);
            return new ViewHolder2(inflate);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        HomeBean.DataBean.DatasBean datasBean = datasBeans.get(position);
        int itemViewType = holder.getItemViewType();
        if (itemViewType == left){
            ViewHolder1 viewHolder1 = (ViewHolder1) holder;
            //Glide.with(context).load(datasBean.getEnvelopePic()).into(viewHolder1.mImg1);
            viewHolder1.mTv1.setText(datasBean.getTitle());
        }else {
            ViewHolder2 viewHolder2 = (ViewHolder2) holder;
            viewHolder2.mTv2.setText(datasBean.getTitle());
           // Glide.with(context).load(datasBean.getEnvelopePic()).into(viewHolder2.mImg2);
        }

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if (onItemLongClick != null){
                    onItemLongClick.ca(view,position);
                }
                return false;
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onItemClick.dj(position);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        if (position %2 == 0){
            return left;
        }else {
            return right;
        }
    }

    @Override
    public int getItemCount() {
        return datasBeans.size();
    }

    public void shanchu(int mPosition) {
        datasBeans.remove(datasBeans.get(mPosition));
        notifyDataSetChanged();
    }

    public class ViewHolder1 extends RecyclerView.ViewHolder {

        ImageView mImg1;
        TextView mTv1;

        public ViewHolder1(View itemView) {
            super(itemView);

            mImg1 = itemView.findViewById(R.id.item1_home_img);
            mTv1 = itemView.findViewById(R.id.item1_home_tv);

        }
    }

    public class ViewHolder2 extends RecyclerView.ViewHolder {

        TextView mTv2;
        ImageView mImg2;

        public ViewHolder2(View itemView) {
            super(itemView);

            mTv2 = itemView.findViewById(R.id.item2_tv);
            mImg2 = itemView.findViewById(R.id.item2_img);

        }
    }

    public interface onItemLongClick{
        void ca(View view,int i);
    }

    onItemLongClick onItemLongClick;

    public void setOnItemLongClick(RlvHomeAdapter.onItemLongClick onItemLongClick) {
        this.onItemLongClick = onItemLongClick;
    }

    public interface onItemClick{
        void dj(int i);
    }
    onItemClick onItemClick;

    public void setOnItemClick(RlvHomeAdapter.onItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }
}